


Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_divisionRoot
    Inherits Csla.BusinessBase(Of rw_divisionRoot)

#Region " Business Properties and Methods "

    ' declare members
    Private _divisionId As Integer = 0
    Private _leagueId As Integer = 0
    Private _divisionName As String = String.Empty
    Private _divisionDescription As String = String.Empty

    ''' <summary>
    ''' Get the DivisionId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the DivisionId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, True)> _
    Public ReadOnly Property DivisionId() As Integer
        Get
            CanReadProperty("DivisionId", True)
            Return _divisionId
        End Get
    End Property

    Public Property leagueId() As Integer
        Get
            CanReadProperty("LeagueId", True)
            Return _leagueId
        End Get
        Set(ByVal value As Integer)
            CanWriteProperty("LeagueId", True)
            If value <> _leagueId Then
                _leagueId = value
                PropertyHasChanged("LeagueId")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the DivisionName of the current object.
    ''' </summary>
    ''' <value>A String that represents the DivisionName of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property DivisionName() As String
        Get
            CanReadProperty("DivisionName", True)
            Return _divisionName
        End Get
        Set(ByVal value As String)
            CanWriteProperty("DivisionName", True)
            If value Is Nothing Then value = String.Empty
            If Not _divisionName.Equals(value) Then
                _divisionName = value
                PropertyHasChanged("DivisionName")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the DivisionDescription of the current object.
    ''' </summary>
    ''' <value>A String that represents the DivisionDescription of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property DivisionDescription() As String
        Get
            CanReadProperty("DivisionDescription", True)
            Return _divisionDescription
        End Get
        Set(ByVal value As String)
            CanWriteProperty("DivisionDescription", True)
            If value Is Nothing Then value = String.Empty
            If Not _divisionDescription.Equals(value) Then
                _divisionDescription = value
                PropertyHasChanged("DivisionDescription")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _divisionId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' DivisionName rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "DivisionName")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("DivisionName", 50))
        ' DivisionDescription rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "DivisionDescription")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("DivisionDescription", 4000))
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in rw_divisionRoot
        'AuthorizationRules.AllowRead("DivisionId", "rw_divisionRootReadGroup")
        'AuthorizationRules.AllowRead("DivisionName", "rw_divisionRootReadGroup")
        'AuthorizationRules.AllowRead("DivisionDescription", "rw_divisionRootReadGroup")

        'AuthorizationRules.AllowWrite("DivisionName", "rw_divisionRootWriteGroup")
        'AuthorizationRules.AllowWrite("DivisionDescription", "rw_divisionRootWriteGroup")
    End Sub

    Public Shared Function CanGetObject() As Boolean
        'TODO: Define CanGetObject permission in rw_divisionRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_divisionRootViewGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanAddObject() As Boolean
        'TODO: Define CanAddObject permission in rw_divisionRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_divisionRootAddGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanEditObject() As Boolean
        'TODO: Define CanEditObject permission in rw_divisionRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_divisionRootEditGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanDeleteObject() As Boolean
        'TODO: Define CanDeleteObject permission in rw_divisionRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_divisionRootDeleteGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

    Private Sub New()
        ' require use of factory method 
    End Sub

    Public Shared Function NewDivisionRoot(ByVal divisionId As Integer, ByVal leagueId As Integer) As rw_divisionRoot
        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_divisionRoot")
        End If
        Return DataPortal.Create(Of rw_divisionRoot)(New Criteria(divisionId, leagueId))
    End Function

    Public Shared Function GetDivisionRoot(ByVal divisionId As Integer, ByVal leagueId As Integer) As rw_divisionRoot
        If Not CanGetObject() Then
            Throw New System.Security.SecurityException("User not authorized to view a rw_divisionRoot")
        End If
        Return DataPortal.Fetch(Of rw_divisionRoot)(New Criteria(divisionId, leagueId))
    End Function

    Public Shared Sub DeleteDivisionRoot(ByVal divisionId As Integer, ByVal leagueId As Integer)
        If Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_divisionRoot")
        End If
        DataPortal.Delete(New Criteria(divisionId, leagueId))
    End Sub

    Public Overrides Function Save() As rw_divisionRoot
        If IsDeleted AndAlso Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_divisionRoot")
        ElseIf IsNew AndAlso Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_divisionRoot")
        ElseIf Not CanEditObject() Then
            Throw New System.Security.SecurityException("User not authorized to update a rw_divisionRoot")
        End If
        Return MyBase.Save()
    End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Criteria "

    <Serializable()> _
    Private Class Criteria

        Public DivisionId As Integer
        Public LeagueId As Integer

        Public Sub New(ByVal divisionId As Integer, ByVal leagueId As Integer)
            Me.DivisionId = divisionId
            Me.LeagueId = leagueId
        End Sub
    End Class

#End Region 'Criteria

#Region " Data Access - Create "

    <RunLocal()> _
    Protected Overloads Sub DataPortal_Create(ByVal criteria As Object)
        ValidationRules.CheckRules()
    End Sub

#End Region ' Data Access - Create

#Region " Data Access - Fetch "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteFetch(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_getDivisionInfo"

            cm.Parameters.AddWithValue("@iFilter", 1)
            cm.Parameters.AddWithValue("@leagueId", criteria.leagueId)
            cm.Parameters.AddWithValue("@divisionId", criteria.DivisionId)

            Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                FetchObject(dr)
                ValidationRules.CheckRules()

                'load child object(s)
                FetchChildren(dr)
            End Using
        End Using
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        dr.Read()
        _divisionId = dr.GetInt32("divisionId")
        _divisionName = dr.GetString("divisionName")
        _divisionDescription = dr.GetString("divisionDescription")
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Protected Overrides Sub DataPortal_Insert()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteInsert(cn)

            'update child object(s)
            UpdateChildren(cn)
        End Using
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_insDivisionInfo"

            AddInsertParameters(cm)

            cm.ExecuteNonQuery()

            _divisionId = CType(cm.Parameters("@newDivisionId").Value, Integer)
        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@leagueId", _leagueId)
        cm.Parameters.AddWithValue("@divisionName", _divisionName)
        cm.Parameters.AddWithValue("@divisionDescription", _divisionDescription)
        cm.Parameters.AddWithValue("@newDivisionId", _divisionId)
        cm.Parameters("@newDivisionId").Direction = ParameterDirection.Output
    End Sub

#End Region 'Data Access - Insert

#Region " Data Access - Update "

    Protected Overrides Sub DataPortal_Update()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            If MyBase.IsDirty Then
                ExecuteUpdate(cn)
            End If

            ' update child object(s)
            UpdateChildren(cn)
        End Using

    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_updDivisionInfo"

            AddUpdateParameters(cm)

            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@divisionId", _divisionId)
        cm.Parameters.AddWithValue("@divisionName", _divisionName)
        cm.Parameters.AddWithValue("@divisionDescription", _divisionDescription)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
    End Sub

#End Region 'Data Access - Update

#Region " Data Access - Delete "

    Protected Overrides Sub DataPortal_DeleteSelf()
        DataPortal_Delete(New Criteria(_divisionId, _leagueId))
    End Sub

    Private Overloads Sub DataPortal_Delete(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteDelete(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_divisionroot_delete"

            cm.Parameters.AddWithValue("@divisionId", criteria.DivisionId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region 'Data Access


End Class

